﻿using System;
using System.Xml;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.RoWebAction;
using Ro.Common.RoInterface.UserType;

namespace Ro.DriverEvent.WebAction
{
    public class WebMouse : IWebMouseFunc
    {
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sigNode">MouseMove节点</param>
        /// <returns>返回是否完成操作</returns>
        public WebActionResult MouseMove(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");

            //构造函数初始化
            FindElement.FindWebElement findElement = new FindElement.FindWebElement(value, timeout);

            IWebElement ele = findElement.WebElement;

            try
            {
                //判断元素是否存在
                if (ele == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "MouseMove操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    Actions action = new Actions(RoTestObject.WebTestObject);
                    action.MoveToElement(ele).Perform();
                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "MouseMove操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("MouseMove", e);
            }
            return back;
        }


        /// <summary>
        /// 鼠标点击操作
        /// </summary>
        /// <param name="sigNode">MouseClick 节点</param>
        /// <returns>返回结果</returns>
        public WebActionResult MouseClick(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            //提取元素
            string value = extractNeedInfo.ExtractAttributeValue(sigNode, "web:RoWebElementID");

            //提取操作类型
            string clicktype = extractNeedInfo.ExtractAttributeValue(sigNode, "web:MouseType");

            //构造函数初始化
            FindElement.FindWebElement findElement = new FindElement.FindWebElement(value, timeout);
            IWebElement ele = findElement.WebElement;

            try
            {
                //判断元素是否存在
                if (ele == null)
                {
                    back = BackActionResult.BackWebResult(false, value, "MouseClick操作遭遇查找元素失败，获取元素为null"); //结果
                }
                else
                {
                    Actions action = new Actions(RoTestObject.WebTestObject);
                    switch (clicktype)
                    {
                        case "ClickLeft":
                            action.Click(ele).Perform();
                            break;

                        case "ClickRight":
                            action.DoubleClick(ele).Perform();
                            break;

                        case "DoubleLeft":
                            action.ContextClick(ele).Perform();
                            break;
                        //右键双击其实挺无聊的
                        case "DoubleRight":
                            action.ContextClick(ele).Perform();
                            action.ContextClick(ele).Perform();
                            break;
                    }

                    back = BackActionResult.BackWebResult(true, value, "没有附加信息"); //结果
                }
            }
            // 抛出异常

            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, value, "MouseClick操作遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("MouseMove", e);
            }
            return back;
        }
    }
}